package lxbAdmin.userSystem.controller;

import lxbAdmin.userSystem.common.ErrorConstants;
import lxbAdmin.userSystem.common.ResponseData;
import lxbAdmin.userSystem.dto.RoleDTO;
import lxbAdmin.userSystem.entity.SystemUserRole;
import lxbAdmin.userSystem.server.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 查询全部角色列表
     *
     * @param roleDTO
     * @return
     */
    @PostMapping("/all")
    public ResponseData getAll(@RequestBody RoleDTO roleDTO) {
        return ResponseData.success(roleService.getAll(roleDTO));
    }

    /**
     * 新增角色
     *
     * @param roleDTO
     * @return
     */
    @PostMapping("/add")
    public ResponseData addRole(@RequestBody RoleDTO roleDTO) {
        List<SystemUserRole> list = roleService.findByOne(roleDTO);
        if (!list.isEmpty())
            return ResponseData.fail(ErrorConstants.WARN_MSG_8);
        return ResponseData.saveSuccess(roleService.addRole(roleDTO));
    }

    /**
     * 修改角色信息
     *
     * @param roleDTO
     * @return
     */
    @PostMapping("/edit")
    public ResponseData editRole(@RequestBody RoleDTO roleDTO) {
        List<SystemUserRole> list = roleService.findByOne(roleDTO);
        if (!list.isEmpty())
            return ResponseData.fail(ErrorConstants.WARN_MSG_8);
        roleService.editRole(roleDTO);
        return ResponseData.editSuccess(roleDTO.getRoleId());
    }

    /**
     * 查询单个角色数据
     *
     * @param roleId
     * @return
     */
    @GetMapping("/detail")
    public ResponseData selectByRoleId(@RequestParam("roleId") String roleId) {
        return ResponseData.success(roleService.selectByRoleId(roleId));
    }


}
